package rssmanager.servlets;

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import rssmanager.service.DBConnect;
import rssmanager.service.FluxRSS;
import rssmanager.service.FluxRSSDAO;
import rssmanager.service.User;

/**
 * Servlet implementation class LogoutServlet
 */
public class ManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManagerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User currentUser = (User) request.getSession().getAttribute("user");
		ResultSet rs;
		String id_user = currentUser.getId().toString();
		String id_flux, nom_flux, lien_flux;
		Map<String, Object> myModel = new HashMap<String, Object>();
		ArrayList<FluxRSS> flux_list = FluxRSSDAO.getInstance().getFluxList();
		flux_list.clear();
		PreparedStatement stmt= null;
		try {
			stmt = DBConnect.getConnection().prepareStatement("SELECT id_flux, nom_flux, lien_flux FROM fluxrss WHERE id_user = ?");
			stmt.setString(1, id_user);
			stmt.execute();
			rs = stmt.getResultSet();
			int rowCount = 0; 
			while (rs.next()) {
				FluxRSS flux_rss = new FluxRSS();
				id_flux =  rs.getString("id_flux");
				nom_flux =  rs.getString("nom_flux");
				lien_flux =  rs.getString("lien_flux");
				flux_rss.setId(Integer.valueOf(id_flux));
				flux_rss.setNom(nom_flux);
				flux_rss.setLien(lien_flux);
				flux_list.add(flux_rss);
				rowCount++;
			}
			request.getSession().setAttribute("flux_list", flux_list);
			request.getSession().setAttribute("nb_flux", flux_list.size());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		response.sendRedirect("manager.jsp");
	}		


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

}